﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Controls;

namespace Nox.Amuse.View
{
    /// <summary>
    /// Interaction logic for AboutBox.xaml
    /// </summary>
    public partial class AboutBox : UserControl
    {
        public AboutBox()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(AboutBox_Loaded);
        }

        void AboutBox_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= new RoutedEventHandler(AboutBox_Loaded);
            var assemblyName = Assembly.GetExecutingAssembly().GetName();
            textName.Text = assemblyName.Name;
            textVersion.Text = assemblyName.Version.ToString();
            textCopyright.Text = "Copyright © 2010 Nidonocu";
            textTier.Text = "Rendering Tier: " + (RenderCapability.Tier >> 16).ToString(CultureInfo.InvariantCulture) +
                "\nSoftware Rendering: " + RenderCapability.IsPixelShaderVersionSupportedInSoftware(2, 0).ToString(CultureInfo.InvariantCulture) +
                "\nPixel Shader Supported: " + RenderCapability.IsPixelShaderVersionSupported(2, 0).ToString(CultureInfo.InvariantCulture);
#if DEBUG
            textReleaseName.Text = "Debug Build";
#elif WEEKLY
			textReleaseName.Text = "Weekly Build";
#elif EXPERIMENTAL
			textReleaseName.Text = "EXPERIMENTAL Build";
#endif
            textReleaseStage.Text = "Alpha Stage 3\n'Nidoqueen'";

            webButton.Click += new RoutedEventHandler(webButton_Click);

            //TODO: Proper use of streams, speed this up and load on demand?
            try
            {
                var tRange = new TextRange(rtbCredits.Document.ContentStart, rtbCredits.Document.ContentEnd);
                tRange.Load(new FileStream(@".\Licenses\Credits.rtf", FileMode.Open, FileAccess.Read), DataFormats.Rtf);
            }
            catch (IOException fault)
            {
                System.Diagnostics.Debug.WriteLine("Exception caught: " + fault.Message);
                rtbCredits.Document = new FlowDocument(new Paragraph(new Run("Unable to load Credits.rtf " + fault.Message)));
            }

            try
            {
                var tRange = new TextRange(rtbLicense.Document.ContentStart, rtbLicense.Document.ContentEnd);
                tRange.Load(new FileStream(@".\Licenses\Amuse License.txt", FileMode.Open, FileAccess.Read), DataFormats.Text);
            }
            catch (IOException fault)
            {
                System.Diagnostics.Debug.WriteLine("Exception caught: " + fault.Message);
                rtbLicense.Document = new FlowDocument(new Paragraph(new Run("Unable to load Amuse License.txt " + fault.Message)));
            }
            try
            {
                var tRange = new TextRange(rtbLicenseProxy.Document.ContentStart, rtbLicenseProxy.Document.ContentEnd);
                tRange.Load(new FileStream(@".\Licenses\Mentalis License.txt", FileMode.Open, FileAccess.Read), DataFormats.Text);
            }
            catch (IOException fault)
            {
                System.Diagnostics.Debug.WriteLine("Exception caught: " + fault.Message);
                rtbLicenseProxy.Document = new FlowDocument(new Paragraph(new Run("Unable to load Mentalis License.txt " + fault.Message)));
            }
            try
            {
                var tRange = new TextRange(rtbLicenseMvvm.Document.ContentStart, rtbLicenseMvvm.Document.ContentEnd);
                tRange.Load(new FileStream(@".\Licenses\MVVM Foundation License.txt", FileMode.Open, FileAccess.Read), DataFormats.Text);
            }
            catch (IOException fault)
            {
                System.Diagnostics.Debug.WriteLine("Exception caught: " + fault.Message);
                rtbLicenseMvvm.Document = new FlowDocument(new Paragraph(new Run("Unable to load MVVM License.txt " + fault.Message)));
            }
        }

        void webButton_Click(object sender, RoutedEventArgs e)
        {
            //TODO: Task Dialog on failure
            try
            {
                Process.Start("http://www.codeplex.com/amuse");
            }
            catch (Win32Exception winFault)
            {
                System.Diagnostics.Debug.WriteLine("Caught Win32Exception: " + winFault.Message);
                MessageBox.Show("Unable to start web browser");
            }
        }
    }
}
